package pers.corvey.crawler.transformer;

import java.util.LinkedList;
import java.util.List;

import pers.corvey.crawler.model.Celebrity;
import pers.corvey.crawler.model.json.CelebrityJson;
import pers.corvey.crawler.model.json.MovieJson;
import pers.corvey.crawler.model.json.WorkJson;

public class CelebrityTransformer {

	public static Celebrity toCelebrity(CelebrityJson json) {
		if (json == null) {
			return null;
		}
		Celebrity celebrity = new Celebrity();
		celebrity.setId(json.getId());
		celebrity.setName(json.getName());
		celebrity.setEnglishName(json.getName_en());
		if (json.getAvatars() != null) {
			celebrity.setImage(json.getAvatars().getLarge());
		}
		celebrity.setGender(json.getGender());
		celebrity.setBornPlace(json.getBorn_place());
		if (json.getWorks() != null) {
			List<MovieJson> movies = new LinkedList<>();
			for (WorkJson work : json.getWorks()) {
				movies.add(work.getSubject());
			}
			celebrity.setWorks(MovieTransformer.toMovie(movies));
		}
		return celebrity;
	}
	
	public static List<Celebrity> toCelebrity(List<CelebrityJson> jsons) {
		if (jsons == null || jsons.size() == 0) {
			return null;
		}
		List<Celebrity> celebrities = new LinkedList<>();
		for (CelebrityJson json : jsons) {
			if (json.getId() != null && json.getName() != null) {
				celebrities.add(toCelebrity(json));
			}
		}
		return celebrities;
	}
	
}
